package com.example.asus.yuejian_10.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.asus.yuejian_10.PagerSlidingTabStrip;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.bean.ClubList;
import com.example.asus.yuejian_10.fragment.ClubClassFragment;
import com.example.asus.yuejian_10.fragment.ClubInfoFragment;
import com.example.asus.yuejian_10.fragment.ClubVideoFragment;
import com.squareup.picasso.Picasso;

import static com.example.asus.yuejian_10.Config.PIC_URL;

public class ClubActivity extends AppCompatActivity {
    private TextView club_name;
    private ImageView club_pic;



    private ClubList club;
    private Context context;

    private LayoutInflater layoutInflater;

    private ClubInfoFragment clubInfoFragment;
    private ClubClassFragment clubClassFragment;
    private ClubVideoFragment clubVideoFragment;

    /**
     * PagerSlidingTabStrip的实例
     */
    private PagerSlidingTabStrip tabs;

    /**
     * 获取当前屏幕的密度
     */
    private DisplayMetrics dm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
        //View ItemView = layoutInflater.inflate(R.layout.item_club,);
        club_name = (TextView) findViewById(R.id .club_name);
        /*club_phone = (TextView) findViewById(R.id .club_phone);
        club_intro = (TextView) findViewById(R.id .club_intro);*/
        club_pic = (ImageView) findViewById(R.id .club_pic);

        club = getIntent().getParcelableExtra("club");

        if(club!=null){
            club_name.setText(club.getClub_name());
            Picasso.with(context)
                    .load(PIC_URL+club.getThumb())
                    .into(club_pic);
        }

        initView();
    }

    private void initView() {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.train_tabs);
        pager.setAdapter(new
                MyPagerAdapter(getSupportFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();
    }

    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(false);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#393a33"));// #45c01a
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor("#393a33"));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    // FragmentPagerAdapter FragmentStatePagerAdapter //不能用FragmentPagerAdapter


    public class MyPagerAdapter extends
            FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = { "俱乐部简介", "精品课程", "相关视频"};

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }


        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:

                    if (null == clubInfoFragment) {
                        clubInfoFragment = new ClubInfoFragment();
                    }

                    return clubInfoFragment;

                case 1:

                    if (null == clubClassFragment) {
                        clubClassFragment = new ClubClassFragment();
                    }

                    return clubClassFragment;
                case 2:

                    if (null == clubVideoFragment) {
                        clubVideoFragment = new ClubVideoFragment();
                    }
                    clubInfoFragment = new ClubInfoFragment();
                    return clubVideoFragment;

                default:


                    return null;
            }
        }

    }

}
